package com.example.anday3;

import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.anday3.adapter.GoodsAdapter;
import com.example.anday3.pojo.GoodsResData;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class GoodsActivity extends AppCompatActivity {
    ListView listView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_goods);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        listView=findViewById(R.id.goodslist);
        getList();
    }

    private void getList() {
        OkHttpClient http=new OkHttpClient();
        Request request=new Request.Builder().url("http://123.56.141.187:8001/goods/index?pagenum=1&pagesize=10").build();
        http.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                Log.e("sls",e.toString());
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
               String str=response.body().string();
                GoodsResData goodsResData=GoodsResData.objectFromData(str);  //json 转成java类
           List<GoodsResData.ListGoods> list= goodsResData.getList();
                GoodsAdapter goodsAdapter=new GoodsAdapter(GoodsActivity.this,list);
                runOnUiThread(()->{
                    listView.setAdapter(goodsAdapter);
                });

            }
        });
    }
}